/* *
 * Copyright (c) 2004-2005 Mocha Almond Fudge AI, SIT KMUTT. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of Mocha Almond Fudge AI, SIT KMUTT nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 */
package jboard.model.ai;

import java.util.Vector;

import jboard.model.BoardPosition;
import jboard.model.HeuristicMovement;
import jboard.model.IBoardConstant;
import jboard.model.Move;
import jboard.model.ai.search.MinimaxNode;
import jboard.model.ai.search.MinimaxTree;

/**
 * 3 Pile look up (agreesive) with position strategy in mind AI
 *
 * @author -NOP-
 */
public class Minimax2AI extends PositionAI implements AI, IBoardConstant {
    private Vector bestMoveList; // Vector of HeuristicMovement
    private MinimaxTree gameTree; // Main concept of this AI
	private BoardPosition myBoardPS;
	private Move myMove; 
	private int strategy = STAIR_LEFT_FORMAT; // Default strategy
	
	public Minimax2AI(int strategy, boolean isReverseStrategy) {
		/** PositionAI section */
		super(); // call constructor of PositionAI
		this.strategy = strategy;
		loadStrategy(this.strategy, isReverseStrategy);
		/** MinimaxAI section */
	    gameTree = new MinimaxTree(THREE_PILE);
	    gameTree.setDepth(2);
	}

    /* (non-Javadoc)
     * @see jboard.model.ai.AI#getMoved()
     */
    public Move getMoved() {
        return this.myMove;
    }
	
	private HeuristicMovement makeMinimaxMove(MinimaxNode minimaxRootNode) {
	    gameTree.setRoot(minimaxRootNode);
	    gameTree.constructTree();
	    bestMoveList = gameTree.getBestPath();
	    
	    return (HeuristicMovement)bestMoveList.get(0);
	}

    /* (non-Javadoc)
     * @see jboard.model.ai.AI#makeMove(jboard.model.BoardPosition)
     */
    public BoardPosition makeMove(BoardPosition currentBPS) {
        // 1. Set BoardPosition so AI can lookup in his own board
        setMyBoardPS(currentBPS);
        // 2. See if has strategy move AND heuristic move
        if (hasStrategyMove() && isStrategicValidMove(this.myBoardPS)) {
        	HeuristicMovement strategyMove = makeStrategyMove();
        	HeuristicMovement minimaxMove = makeMinimaxMove((prepareRootNode(this.myBoardPS)));
        	int strategicH = strategyMove.getHeuristicValue();
        	int minimaxH = minimaxMove.getHeuristicValue();
        	// Compare heuristic val
        	if(strategicH > minimaxH) {
        		// Do strategy move
        		this.myMove = strategyMove.getMove();
        	} else {
        		// Do minimax move
        		this.myMove = minimaxMove.getMove();
        	}
        } else {
        	// Not have strategy left
        	this.myMove = makeMinimaxMove((prepareRootNode(this.myBoardPS))).getMove();
        }
        
		return myBoardPS.makeMove(this.myMove, BLACK);
    }
	
	private HeuristicMovement makeStrategyMove() {
		return getStrategyMove(this.myBoardPS);
	}
	
	private MinimaxNode prepareRootNode(BoardPosition boardPosition) {
	    return new MinimaxNode(boardPosition, 0, 0); // (0, 0) first mean no nedd to calc Heuristic Val, second mean level 0 <MAX>
	}

    private void setMyBoardPS(BoardPosition myBoardPS) {
        this.myBoardPS = myBoardPS;
    }
}
